import json

def get_stored_username():
    """如果存储了用户名，就获取它"""
    filename = 'username.json'
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        return None

def get_new_username():
    """提示用户输入新用户名并存储"""
    username = input("What is your name? ")
    filename = 'username.json'
    with open(filename, 'w', encoding='utf-8') as f:
        json.dump(username, f,ensure_ascii=False)
    return username

def greet_user():
    """问候用户，并验证身份是否正确"""
    username = get_stored_username()
    
    if username:
        # 验证用户名是否正确
        response = input(f"Are you {username}? (yes/no) ")
        if response.lower() in ('yes', 'y'):
            print(f"Welcome back, {username}!")
        else:
            # 用户名不正确，获取新用户名
            username = get_new_username()
            print(f"We'll remember you when you come back, {username}!")
    else:
        # 没有存储的用户名，获取新用户名
        username = get_new_username()
        print(f"We'll remember you when you come back, {username}!")

# 运行程序
greet_user()
